分类
联系方式
  1. 新浪微博
  2. E-mail

Dart eval:DartCompilationUnit 实体类

介绍

该类表示一个经过解析后的 Dart 源文件(AST),但是尚未被解析为库(library)。在 Dart 中,每个源文件都是一个库(library)。DartCompilationUnit 是源代码编译的一个中间阶段,表示从源代码经过 Dart Analyzer 转换为 AST。最终 DartCompilationUnit 是要转换为 Library 实体的。

代码实现

具体代码实现如下:

class DartCompilationUnit {
  DartCompilationUnit(this.uri,
      {required this.imports,
      required this.exports,
      required this.parts,
      required this.declarations,
      this.library,
      this.partOf});

  /// A `package`, `dart`, or `file` URI
  // 包的路径标识
  final Uri uri;

  /// Library 指令,有的源码开头的 "library ******"
  final LibraryDirective? library;
  /// 对应于 part of 语法,只支持一个
  final PartOfDirective? partOf;

  /// 包导入
  final List<ImportDirective> imports;
  /// 包导出
  final List<ExportDirective> exports;
  /// part 语法
  /// part 与 part of 的关系:https://stackoverflow.com/questions/67096135/how-to-use-part-or-part-of-in-dart
  final List<PartDirective> parts;

  /// 各种具体的代码声明
  final List<CompilationUnitMember> declarations;

  @override
  bool operator ==(Object other) =>
      identical(this, other) || other is DartCompilationUnit && runtimeType == other.runtimeType && uri == other.uri;

  @override
  int get hashCode => uri.hashCode;
}

从中可以看出,一个 Libarary 相关的组成部分,都在 DartCompilationUnit 通过不同属性标识了。